
// make sure we only arttach the onload event one time
//debugger;
var fixPresenceAlreadyRun;
if (!(fixPresenceAlreadyRun && fixPresenceAlreadyRun == true)) {
    window.attachEvent("onload", FixPresence);
    fixPresenceAlreadyRun = true;
}

// this is either called by onload of the document or by the outer div tag of the comment html
function FixPresence(e, rootElement) {
    //debugger;
    /*
    if (fixPresenceAlreadyRun && fixPresenceAlreadyRun == true)
        return;
    fixPresenceAlreadyRun = true;
    */

    if (!e) var e = window.event;
    if (!rootElement)
        rootElement = document;
    
    var containedDivElements = rootElement.getElementsByTagName("div");
    for (var i = 0; i < containedDivElements.length; i++) {
        var div = containedDivElements[i];
        // ignore tags that were not created by our own tool
        if (div && div.className == 'LoggedCommentHistoryHeader') {
            var containedAnchors = div.getElementsByTagName("a");
            if (containedAnchors) {
                // test to see if icon is before or after the name link
                var iconAfterName = false;
                var anchorTest = containedAnchors[1];
                if (anchorTest && anchorTest.className == "ms-imnlink")
                    iconAfterName = true;
                // get the name link                    
                var anchorGotoLink = containedAnchors[iconAfterName ? 0 : 1];
                if (anchorGotoLink) {
                    anchorGotoLink.onclick = function() { GoToLink(this);return false; }
                    if (anchorGotoLink.captureEvents)
                        anchorGotoLink.captureEvents(Event.CLICK);
                }
                // get the icon link
                var anchorImageOnClick = containedAnchors[iconAfterName ? 1 : 0];
                if (anchorImageOnClick) {
                    anchorImageOnClick.onclick = function() { IMNImageOnClick();return false; }
                    if (anchorImageOnClick.captureEvents)
                        anchorImageOnClick.captureEvents(Event.CLICK);
                }
                // dig into the image inside the icon link
                var containedImgs = anchorImageOnClick.getElementsByTagName("img");
                if (containedImgs) {
                    var img = containedImgs[0];
                    if (!img) {
                        //there's really no way to add this here, because we lack the information we would need to fill in the variables
                        //anchorImageOnClick.innerHtml = "<img title=\"encodedUserEmail\" alt=\"No presence information\" name=\"imnmark\" border=\"0\" valign=\"middle\" height=\"12\" width=\"12\" src=\"/_layouts/images/imnhdr.gif\" sip=\"user.Email\" id=\"imn#,type=sip\" />";
                    } else {
                        if (img.alt && img.alt == "No presence information") {
                            img.setAttribute('name', 'imnmark');
                            var title = img.getAttribute('title');
                            var emailAddy = title.replace(/#/, '@');
                            img.setAttribute('sip', emailAddy);
                            img.setAttribute('title', '');
                            IMNRC(emailAddy, img);   
                        }
                    } // img
                } // containedImgs
            } // containedAnchors
        } // div && div.className
    } // for i
} // function FixPresence
